/*******************************************************************************
 * Copyright (c) 2008 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.classdiagram.parsers.texteditor.propertylabel;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.classdiagram.parsers.modelgenerator.PropertyGenerator;
import com.cea.papyrus.extensionpoints.editors.configuration.DefaultDirectEditorConfiguration;

/**
 * Configuration Class for the direct edition of a UML property
 */
public class PropertyConfigurationForUML extends DefaultDirectEditorConfiguration  {

	/** configuration of the source viewer */
	final private PropertyLabelSourceViewerConfiguration configuration;

	/**
	 * Creates a new PropertyConfigurationForUML.
	 */
	public PropertyConfigurationForUML() {
		configuration = new PropertyLabelSourceViewerConfiguration();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public SourceViewerConfiguration getSourceViewerConfiguration() {
		return configuration;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getTextToEdit(Object editedObject) {
		if(editedObject instanceof Property) {
			com.cea.papyrus.umlutils.Property utilProperty = new com.cea.papyrus.umlutils.Property((Property)editedObject);
			return utilProperty.getLabel();
		}
		return "not a Property";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object preEditAction(Object objectToEdit) {
		if(objectToEdit instanceof Property) {
			configuration.setProperty((Property)objectToEdit);
		}
		return super.preEditAction(objectToEdit);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Object postEditAction(Object editedObject, String text) {
		// should undo the various creations done in the property edition (template binding, etc...)
		if(editedObject instanceof Property) {
			PropertyGenerator generator = new PropertyGenerator(((Property)editedObject));
			generator.parseAndModifyProperty(text);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IInputValidator getInputValidator() {
		return new IInputValidator() {
		
			public String isValid(String newText) {
				if(getObjectToEdit() instanceof Port) {
					PropertyGenerator generator = new PropertyGenerator(((Property)getObjectToEdit()));
					return generator.parseAndValidateProperty(newText);
				}
				return null;
			}
		};
	}
	
}
